package com.wintrue.ffxs.ui.home;

import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wintrue.ffxs.R;
import com.wintrue.ffxs.base.BaseFragmentActivity;

import com.wintrue.ffxs.ui.message.MsgFragment;
import com.wintrue.ffxs.ui.mine.MyFragment;
import com.wintrue.ffxs.ui.shoppingcar.ShopcarFragment;

/**
 * Created by lhe on 7/8/14.
 */

public class MainActivity extends BaseFragmentActivity implements View.OnClickListener{


    FragmentManager fm;
    FragmentTransaction ft;
    HomeFragment homeFragment;
    ShopcarFragment shopcarFragment;
    MsgFragment msgFragment;
    MyFragment myFragment;


    LinearLayout home_ll,shopcar_ll,my_ll,msg_ll;
    ImageView home_img,shopcar_img,my_img,msg_img;
    TextView home_tv,shopcar_tv,my_tv,msg_tv;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }

    private void init() {
        homeFragment=new HomeFragment();
        shopcarFragment=new ShopcarFragment();
        msgFragment=new MsgFragment();
        myFragment=new MyFragment();

        home_ll= (LinearLayout) findViewById(R.id.home_ll);
        shopcar_ll= (LinearLayout) findViewById(R.id.shopcar_ll);
        my_ll= (LinearLayout) findViewById(R.id.my_ll);
        msg_ll= (LinearLayout) findViewById(R.id.msg_ll);

        home_img= (ImageView) findViewById(R.id.home_img);
        shopcar_img= (ImageView) findViewById(R.id.shopcar_img);
        my_img= (ImageView) findViewById(R.id.my_img);
        msg_img= (ImageView) findViewById(R.id.msg_img);

        home_tv= (TextView) findViewById(R.id.home_tv);
        shopcar_tv= (TextView) findViewById(R.id.shopcar_tv);
        my_tv= (TextView) findViewById(R.id.my_tv);
        msg_tv= (TextView) findViewById(R.id.msg_tv);

        home_ll.setOnClickListener(this);
        shopcar_ll.setOnClickListener(this);
        my_ll.setOnClickListener(this);
        msg_ll.setOnClickListener(this);


        //获取fragmentmanager
        fm = getSupportFragmentManager();
        ft = fm.beginTransaction();
        ft.replace(R.id.frl, homeFragment);

        //提交事务
        ft.commit();


    }


    @Override
    public void onClick(View v) {
        //开启事务
        ft = fm.beginTransaction();
        switch(v.getId()){
            case R.id.home_ll:
                ft.replace(R.id.frl, homeFragment);
                clear();
                home_img.setImageResource(R.drawable.homedj);
                home_tv.setTextColor(ActivityCompat.getColor(MainActivity.this, R.color.color_f02f32));
                break;
            case R.id.shopcar_ll:
                ft.replace(R.id.frl, shopcarFragment);
                clear();
                shopcar_img.setImageResource(R.drawable.gwcdj);
                shopcar_tv.setTextColor(ActivityCompat.getColor(MainActivity.this, R.color.color_f02f32));
                break;
            case R.id.msg_ll:
                ft.replace(R.id.frl, msgFragment);
                clear();
                msg_img.setImageResource(R.drawable.icon_xx_dj);
                msg_tv.setTextColor(ActivityCompat.getColor(MainActivity.this, R.color.color_f02f32));
                break;
            case R.id.my_ll:
                ft.replace(R.id.frl, myFragment);
                clear();
                my_img.setImageResource(R.drawable.wddj);
                my_tv.setTextColor(ActivityCompat.getColor(MainActivity.this, R.color.color_f02f32));
                break;
        }
        //提交事务
        ft.commit();
    }


    public void clear(){
        home_img.setImageResource(R.drawable.homewdj);
        home_tv.setTextColor(ActivityCompat.getColor(MainActivity.this,R.color.color_4d4d4d));
        shopcar_img.setImageResource(R.drawable.gwcwdj);
        shopcar_tv.setTextColor(ActivityCompat.getColor(MainActivity.this,R.color.color_4d4d4d));
        my_img.setImageResource(R.drawable.wdwdj);
        my_tv.setTextColor(ActivityCompat.getColor(MainActivity.this,R.color.color_4d4d4d));
        msg_img.setImageResource(R.drawable.icon_xx_wdj);
        msg_tv.setTextColor(ActivityCompat.getColor(MainActivity.this,R.color.color_4d4d4d));

    }
}
